using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using TMPro;

public class GameUiManager : MonoBehaviour
{
    [SerializeField]
    [Tooltip("可以检测的碰撞层")]
    private LayerMask clickableLayer;
    [SerializeField] private LayerMask groundLayer;

    [SerializeField] private CustomProgress progress;
    [SerializeField] private TextMeshProUGUI ratioLabel;

    private Transform curTarget;
    private float totalTime = 0;

    /// <summary>
    /// 进度总时长
    /// </summary>
    private float progressTotalTime = 0;

    private int scoreRatio = 1;
    public int ScoreRatio {
        get {
            return scoreRatio;
        }
        set {
            scoreRatio = value;
            ratioLabel.text = $"X{value}";
        }
    }

    public float ProgressTotalTime {
        get {
            return progressTotalTime;
        }
        set {
            progressTotalTime = value;
        }
    }

    private bool showProgress = false;
    public bool ShowProgress {
        get {
            return showProgress;
        }
        set {
            showProgress = value;
        }
    }

    private void Awake()
    {
        GameManager.Instance().uiManager = this;
    }

    public void Restart() {
        ScoreRatio = 1;
        progress.Progress = 0f;
        totalTime = 0;
        progressTotalTime = 0;
    }

    void OnEnable() {
        EventManager.Instance.Sub("clear",ClearEvent);
    }

    void OnDisable() {
        EventManager.Instance.UnSub("clear",ClearEvent);
    }

    public void ClearEvent(object data) {
        ScoreRatio++;
        float? time = GameManager.Instance().game.GetTimeByRatio(scoreRatio);
        progressTotalTime = time ?? 0.1f;
        totalTime = 0;
        progress.Progress = 1f;
        ShowProgress = true;
    }

    public void UpdateProgress(float dt) {
        totalTime += dt;
        if(totalTime >= ProgressTotalTime) {
            // 重置倍数
            ScoreRatio = 1;
            // 重置完毕不再显示进度
            ShowProgress = false;
        }
        progress.Progress = 1f - (totalTime / ProgressTotalTime);
    }

    private void Update()
    {
        if(ShowProgress) {
            UpdateProgress(Time.deltaTime);
        }

        if (Input.touchCount > 0)
        {
            Touch touch = Input.GetTouch(0);
            if(EventSystem.current.IsPointerOverGameObject(touch.fingerId)) {
                Debug.Log("此事件发生在UI上");
                return;
            }
            if (touch.phase == TouchPhase.Began)
            {
                touchBegan(touch);
            }
            else if (touch.phase == TouchPhase.Moved)
            {
                touchMove(touch);
            }
            else
            {
                touchEnd(touch);
            }
        }
    }

    private void touchBegan(Touch touch)
    {
        Ray ray = Camera.main.ScreenPointToRay(touch.position);
        RaycastHit hit;

        if (Physics.Raycast(ray, out hit, Mathf.Infinity, clickableLayer))
        {
            UnityEngine.Debug.Log("hit: " + hit.collider.name);
            curTarget = hit.collider.transform;
        }
    }

    private void touchMove(Touch touch)
    {
    }

    private void touchEnd(Touch touch)
    {

        if (curTarget != null)
        {
            EventManager.Instance.Pub("hit", curTarget.GetComponent<MeshCollider>());
        }
    }

    void Start()
    {
        Debug.Log("槽子UI是否是root canvas: " + GetComponent<Canvas>().isRootCanvas);
    }
}
